package com.petalsgroup.petals.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class RecentlyUtil {
    private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static DateFormat htmlFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getRecentlyTimeStr(int day) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - day);
        date = calendar.getTime();
        return format.format(date);
    }

    public static String getRecentlyDayTimeStr(int day) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - day);
        date = calendar.getTime();
        return format.format(date);
    }

    public static String getTimeAddStr(Date time,int day) {
        Date date = time;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - day);
        date = calendar.getTime();
        return format.format(date);
    }

    public static Date getDate(String str){
        try {
            return format2.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getStr(Date date){
        return htmlFormat.format(date);
    }
}
